package fr.esgi.pages;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.esgi.classes.Livrable;
import fr.esgi.classes.Projet;
import fr.esgi.donnees.Database;
import fr.esgi.utils.Utils;



public class ValidationProjet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ValidationProjet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Database d=new Database();
		Utils u=new Utils();
		PrintWriter out = response.getWriter();
		String nomLivrable=request.getParameter("livrableNom");
		String descriptionLivrable=request.getParameter("livrableDescription");
		String date=request.getParameter("livrableDate");
		String 	tabNom[]=nomLivrable.split(";");
		String []tabdescription=descriptionLivrable.split(";");
		String []tabDate=date.split(";");
		String name=request.getParameter("name");
		String  description=request.getParameter("description");
		String promotion=request.getParameter("promotion");
		String  groupe=request.getParameter("groupe");
		String  constitution=request.getParameter("constitution");
		String  professeur=request.getParameter("professeur");
		out.print(professeur);
		String  nb=request.getParameter("nb");
		int gr;
		if(groupe!=null)
			gr=Integer.parseInt(groupe);
		else gr=0; 
		try {
			if(!u.checkNom(name)||!u.checkSize(name)){
				request.setAttribute("erreur", "probleme sur le nom.");
				getServletContext().getRequestDispatcher("/jsp/pages/creationProjet.jsp").forward(request, response);
			}
			else if(!u.checkNom(description)||!u.checkSize(description)){
				request.setAttribute("erreur", "probleme sur la description.");
				getServletContext().getRequestDispatcher("/jsp/pages/creationProjet.jsp").forward(request, response);
			} else if(!d.UniqueProjet(name)){
				request.setAttribute("erreur", "projet doit etre unique");
				getServletContext().getRequestDispatcher("/jsp/pages/creationProjet.jsp").forward(request, response);
			}
			else{
				Projet p=new Projet(name,description,professeur,Integer.parseInt(nb),promotion,gr,constitution);
				d.insertProjet(p);
				Projet p2=d.buildProject(name);
				for(int i=0;i<tabNom.length;i++){
					Livrable l=new Livrable( tabNom[i], tabdescription[i],tabDate[i],p2.getId(),i+1);
					d.insertLivrable(l);
				}
				if(p.getCompositionGroupes().equals("aleatoire")){
					
					System.out.println(String.valueOf(p2.getId()));
					request.setAttribute("projet",String.valueOf(p2.getId()));
					request.setAttribute("promotion",p2.getPromotion());
					request.setAttribute("groupe",String.valueOf(p2.getGroupe()));
					request.setAttribute("nb",String.valueOf(p2.getNbEtudiantRequis()));
					getServletContext().getRequestDispatcher("/jsp/pages/creationGroupeAleatoire.jsp").forward(request, response);
				}
			}		
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
